package controller.teacher.manager.score;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

import controller.score.Base;
import dao.Score;

@WebServlet("/teacher/manager/score/edit")
public class edit extends Base {
    private static final long serialVersionUID = 1L;

    public edit() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        try {
            Integer id = new Integer(request.getParameter("id"));
            Score.Data d = Score.findById(id);
            view.displayMap("teacher/score/edit.jsp", "data", d);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        try {
            Integer id = new Integer(request.getParameter("id"));
            Integer stud = new Integer(request.getParameter("stud"));
            PreparedStatement stmt = Score.update("`id`=" + id, "subject", "score");
            stmt.setString(1, request.getParameter("subject"));
            stmt.setBigDecimal(2, new BigDecimal(request.getParameter("score")));
            stmt.executeUpdate();
            view.sendRedirect(request.getContextPath() + "/teacher/manager/score?id="+stud);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
